﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.MemberInfo
{
    public partial class MemberInfoManage : PageBaseAdmin
    {
        #region 固定方法

        readonly D_T_Member_Exp _Dal = new D_T_Member_Exp();

        private void SetBut(int i, Button but)
        {
            switch(i)
            {
                case 1:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 2:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                case 3:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 4:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                default:
                    break;
            }
        }
        private huaguwang.WebControls.WebUserToolBar.SelectItem[] SetText()
        {
            huaguwang.WebControls.WebUserToolBar.SelectItem[] list = { 
                new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>TextBox1.Text.Exp_Trim(),
                    FuncSetText=V=>{TextBox1.Text=V;},
                    Name=TextBox1.ID
                },                                                                     
                   new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>this.DropDownList1.SelectedValue,
                    FuncSetText=V=>{this.DropDownList1.SelectedValue=V;},
                    Name=DropDownList1.ID
                }, 
                new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>this.DropDownList2.SelectedValue,
                    FuncSetText=V=>{this.DropDownList2.SelectedValue=V;},
                    Name=DropDownList2.ID
                }, 
                new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>this.DropDownList3.SelectedValue,
                    FuncSetText=V=>{this.DropDownList3.SelectedValue=V;},
                    Name=DropDownList3.ID
                }, 
                                                                     
            };
            return list;
        }
        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {
                var p1 = WebUserToolBar1.MainGetQuery(TextBox1.ID);
                var p2 = WebUserToolBar1.MainGetQuery(DropDownList1.ID);
                var p3 = WebUserToolBar1.MainGetQuery(DropDownList2.ID).Exp_IntTryParse();
                var p4 = WebUserToolBar1.MainGetQuery(DropDownList3.ID).Exp_IntTryParse();

                bool? p2_1 = null;
                if(p2 == "1")
                {
                    p2_1 = true;
                }
                if(p2 == "2")
                {
                    p2_1 = false;
                }


                Pager.RecordCount = _Dal.FindListCount(p1, p2_1, p3, p4);
                this.Repeater1.DataSource = _Dal.
                        FindList(begin, Pager.PageSize, p1, p2_1, p3, p4);
                this.Repeater1.DataBind();
            });
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                Pager.PageSize = 10;



                if(!IsPostBack)
                {

                    this.DropDownList1.Items.Add(new ListItem() { Value = "0", Text = "所有" });
                    this.DropDownList1.Items.Add(new ListItem() { Value = "1", Text = "已验证" });
                    this.DropDownList1.Items.Add(new ListItem() { Value = "2", Text = "未验证" });

                    this.DropDownList2.Items.Add(new ListItem() { Value = "-1", Text = "所有" });
                    this.DropDownList2.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.normal).ToString(),
                            Text = "正常"
                        });
                    this.DropDownList2.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.freeze).ToString(),
                            Text = "冻结"
                        });
                    this.DropDownList2.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.foreverfreeze).ToString(),
                            Text = "永久冻结"
                        });
                    this.DropDownList3.Items.Add(new ListItem() { Value = "-1", Text = "所有" });
                    this.DropDownList3.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.normal).ToString(),
                            Text = "正常"
                        });
                    this.DropDownList3.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.freeze).ToString(),
                            Text = "冻结"
                        });
                    this.DropDownList3.Items.Add
                        (new ListItem()
                        {
                            Value = ((int)M_T_Member_Exp.Freeze_Type.foreverfreeze).ToString(),
                            Text = "永久冻结"
                        });

                    //分页查询

                }
                //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
                this.WebUserToolBar1.MainInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr,
                    SetText()
                );
                if(!IsPostBack)
                {
                    SetData(0);
                }
            });



        }




        #region 基本的带有查询的事件组合

        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AdminPublic.Try(() =>
            {
                SetData((e.NewPageIndex - 1) * Pager.PageSize);
            });
        }

        /// <summary>
        /// 详细按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                var strID = Request.Params["HiddenID"].Exp_Trim();
                Response.Redirect("MemberInfoManage_E_Particular.aspx?ID=" + strID + "&" + sKey, false);
            });
        }
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                Response.Redirect("MemberInfoManage_E.aspx?" + sKey, false);
            });
        }

        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var sKey = WebUserToolBar1.MainSetSKey();
                Response.Redirect("MemberInfoManage_E.aspx?ID=" + strID + "&" + sKey, false);
            });
        }
        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
                if(intA == null || intA.Count <= 0)
                {
                    this.Exp_PageAlter("没有找到可以删除的信息");
                    return;
                }

                int i = 0;
                foreach(var item in intA)
                {
                    i++;
                    //检测权限
                    //var tempI = _Dal.GetPowerCount(item);
                    //if (tempI > 0)
                    //{
                    //    this.Exp_PageAlter("当前第" + i + "条资料出现在有效权限组中,不能够直接删除");
                    //    return;
                    //}

                }


                var refInt = _Dal.UpdateFlag(intA);
                if(refInt > 0)
                {
                    this.Exp_PageWriteLog(Log_Type.delete, "会员管理", intA);
                    SetData(0);
                    this.Exp_PageAlter("删除操作成功");
                    return;
                }
                else
                {
                    this.Exp_PageAlter("删除操作失败");
                    return;
                }
            });
            //this.Exp_PageAlter("获得了" + strID);
        }
        #endregion

        //protected void WebUserToolBar1_Button4(object sender, EventArgs e)
        //{
        //    AdminPublic.Try(() =>
        //    {
        //        var strID = "," + Request.Params["HiddenID"].Exp_Trim();
        //        var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
        //        if(intA == null || intA.Count <= 0)
        //        {
        //            this.Exp_PageAlter("没有找到可以操作的信息");
        //            return;
        //        }

        //        int i = 0;
        //        foreach(var item in intA)
        //        {
        //            i++;
        //            //检测权限
        //            //var tempI = _Dal.GetPowerCount(item);
        //            //if (tempI > 0)
        //            //{
        //            //    this.Exp_PageAlter("当前第" + i + "条资料出现在有效权限组中,不能够直接删除");
        //            //    return;
        //            //}

        //        }


        //        var refInt = _Dal.ResetPassWord("", "");
        //        if(refInt > 0)
        //        {
        //            //    this.Exp_PageWriteLog("密码重置成功",
        //            //        "id:{0},共{1}个,成功操作{2}个".Exp_Format(strID, intA.Count, refInt),
        //            //        "用户密码重置",
        //            //        true);
        //            SetData(0);
        //            this.Exp_PageAlter("操作成功");
        //            return;
        //        }
        //        else
        //        {

        //            this.Exp_PageAlter("操作失败");
        //            return;
        //        }
        //    });
        //}



        protected void WebUserToolBar1_Button41(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                var strID = Request.Params["HiddenID"].Exp_Trim();
                Response.Redirect("MemberInfoManage_E_Activity.aspx?ID=" + strID + "&" + sKey, false);

            });
        }

        protected void WebUserToolBar1_Button5(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                WebUserToolBar1.MainSetQuery();
                SetData(0);
            });
        }
    }
}
